﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Clipper.DAL
{
	/// <summary>
	/// 数据访问类:ResultReference
	/// </summary>
	public partial class ResultReference
	{
		public ResultReference()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string DetectItem)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from ResultReference");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20)			};
			parameters[0].Value = DetectItem;

			return SQLiteHelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Clipper.Model.ResultReference model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into ResultReference(");
            strSql.Append("DetectItem,ItemCode,RRsToplimit,RRsLowlimit,HighLimit,LowLimit,unit,qualitativeA,qualitativeB,qualitativeC,qualitativeD,QualitativeFormat,negativeLight,positiveLight,note)");
			strSql.Append(" values (");
            strSql.Append("@DetectItem,@ItemCode,@RRsToplimit,@RRsLowlimit,@HighLimit,@LowLimit,@unit,@qualitativeA,@qualitativeB,@qualitativeC,@qualitativeD,@QualitativeFormat,@negativeLight,@positiveLight,@note)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20),
					new SQLiteParameter("@RRsToplimit", DbType.Decimal,8),
					new SQLiteParameter("@RRsLowlimit", DbType.Decimal,8),
					new SQLiteParameter("@HighLimit", DbType.Decimal,8),
					new SQLiteParameter("@LowLimit", DbType.Decimal,8),
					new SQLiteParameter("@unit", DbType.String,10),
					new SQLiteParameter("@qualitativeA", DbType.Decimal,8),
					new SQLiteParameter("@qualitativeB", DbType.Decimal,8),
					new SQLiteParameter("@qualitativeC", DbType.Decimal,8),
					new SQLiteParameter("@qualitativeD", DbType.Decimal,8),
					new SQLiteParameter("@QualitativeFormat", DbType.Byte,1),
					new SQLiteParameter("@negativeLight", DbType.Decimal,8),
					new SQLiteParameter("@positiveLight", DbType.Decimal,8),
					new SQLiteParameter("@note", DbType.String,255),
                    new SQLiteParameter("@ItemCode",DbType.String,20)};

			parameters[0].Value = model.DetectItem;
            
			parameters[1].Value = model.RRsToplimit;
			parameters[2].Value = model.RRsLowlimit;
			parameters[3].Value = model.HighLimit;
			parameters[4].Value = model.LowLimit;
			parameters[5].Value = model.unit;
			parameters[6].Value = model.qualitativeA;
			parameters[7].Value = model.qualitativeB;
			parameters[8].Value = model.qualitativeC;
			parameters[9].Value = model.qualitativeD;
			parameters[10].Value = model.QualitativeFormat;
			parameters[11].Value = model.negativeLight;
			parameters[12].Value = model.positiveLight;
			parameters[13].Value = model.note;
            parameters[14].Value = model.ItemCode;
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Clipper.Model.ResultReference model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update ResultReference set ");
           
			strSql.Append("RRsToplimit=@RRsToplimit,");
            strSql.Append("ItemCode=@ItemCode,");
			strSql.Append("RRsLowlimit=@RRsLowlimit,");
			strSql.Append("HighLimit=@HighLimit,");
			strSql.Append("LowLimit=@LowLimit,");
			strSql.Append("unit=@unit,");
			strSql.Append("qualitativeA=@qualitativeA,");
			strSql.Append("qualitativeB=@qualitativeB,");
			strSql.Append("qualitativeC=@qualitativeC,");
			strSql.Append("qualitativeD=@qualitativeD,");
			strSql.Append("QualitativeFormat=@QualitativeFormat,");
			strSql.Append("negativeLight=@negativeLight,");
			strSql.Append("positiveLight=@positiveLight,");
			strSql.Append("note=@note");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@RRsToplimit", DbType.Decimal,8),
					new SQLiteParameter("@RRsLowlimit", DbType.Decimal,8),
					new SQLiteParameter("@HighLimit", DbType.Decimal,8),
					new SQLiteParameter("@LowLimit", DbType.Decimal,8),
					new SQLiteParameter("@unit", DbType.String,10),
					new SQLiteParameter("@qualitativeA", DbType.Decimal,8),
					new SQLiteParameter("@qualitativeB", DbType.Decimal,8),
					new SQLiteParameter("@qualitativeC", DbType.Decimal,8),
					new SQLiteParameter("@qualitativeD", DbType.Decimal,8),
					new SQLiteParameter("@QualitativeFormat", DbType.Byte,1),
					new SQLiteParameter("@negativeLight", DbType.Decimal,8),
					new SQLiteParameter("@positiveLight", DbType.Decimal,8),
					new SQLiteParameter("@note", DbType.String,255),
					new SQLiteParameter("@DetectItem", DbType.String,20),
                    new SQLiteParameter("@ItemCode",DbType.String,20)};
			parameters[0].Value = model.RRsToplimit;
			parameters[1].Value = model.RRsLowlimit;
			parameters[2].Value = model.HighLimit;
			parameters[3].Value = model.LowLimit;
			parameters[4].Value = model.unit;
			parameters[5].Value = model.qualitativeA;
			parameters[6].Value = model.qualitativeB;
			parameters[7].Value = model.qualitativeC;
			parameters[8].Value = model.qualitativeD;
			parameters[9].Value = model.QualitativeFormat;
			parameters[10].Value = model.negativeLight;
			parameters[11].Value = model.positiveLight;
			parameters[12].Value = model.note;
			parameters[13].Value = model.DetectItem;
            parameters[14].Value = model.ItemCode;
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string DetectItem)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ResultReference ");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20)			};
			parameters[0].Value = DetectItem;

			int rows=SQLiteHelper.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string DetectItemlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from ResultReference ");
			strSql.Append(" where DetectItem in ("+DetectItemlist + ")  ");
			int rows=SQLiteHelper.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.ResultReference GetModel(string DetectItem)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select DetectItem,ItemCode,RRsToplimit,RRsLowlimit,HighLimit,LowLimit,unit,qualitativeA,qualitativeB,qualitativeC,qualitativeD,QualitativeFormat,negativeLight,positiveLight,note from ResultReference ");
			strSql.Append(" where DetectItem=@DetectItem ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@DetectItem", DbType.String,20)			};
			parameters[0].Value = DetectItem;

			Clipper.Model.ResultReference model=new Clipper.Model.ResultReference();
			DataSet ds=SQLiteHelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.ResultReference DataRowToModel(DataRow row)
		{
			Clipper.Model.ResultReference model=new Clipper.Model.ResultReference();
			if (row != null)
			{
				if(row["DetectItem"]!=null)
				{
					model.DetectItem=row["DetectItem"].ToString();
				}
                if (row["ItemCode"] != null)
                {
                    model.ItemCode = row["ItemCode"].ToString();
                }
				if(row["RRsToplimit"]!=null && row["RRsToplimit"].ToString()!="")
				{
					model.RRsToplimit=decimal.Parse(row["RRsToplimit"].ToString());
				}
				if(row["RRsLowlimit"]!=null && row["RRsLowlimit"].ToString()!="")
				{
					model.RRsLowlimit=decimal.Parse(row["RRsLowlimit"].ToString());
				}
				if(row["HighLimit"]!=null && row["HighLimit"].ToString()!="")
				{
					model.HighLimit=decimal.Parse(row["HighLimit"].ToString());
				}
				if(row["LowLimit"]!=null && row["LowLimit"].ToString()!="")
				{
					model.LowLimit=decimal.Parse(row["LowLimit"].ToString());
				}
				if(row["unit"]!=null)
				{
					model.unit=row["unit"].ToString();
				}
				if(row["qualitativeA"]!=null && row["qualitativeA"].ToString()!="")
				{
					model.qualitativeA=decimal.Parse(row["qualitativeA"].ToString());
				}
				if(row["qualitativeB"]!=null && row["qualitativeB"].ToString()!="")
				{
					model.qualitativeB=decimal.Parse(row["qualitativeB"].ToString());
				}
				if(row["qualitativeC"]!=null && row["qualitativeC"].ToString()!="")
				{
					model.qualitativeC=decimal.Parse(row["qualitativeC"].ToString());
				}
				if(row["qualitativeD"]!=null && row["qualitativeD"].ToString()!="")
				{
					model.qualitativeD=decimal.Parse(row["qualitativeD"].ToString());
				}
				if(row["QualitativeFormat"]!=null && row["QualitativeFormat"].ToString()!="")
				{
					model.QualitativeFormat=int.Parse(row["QualitativeFormat"].ToString());
				}
				if(row["negativeLight"]!=null && row["negativeLight"].ToString()!="")
				{
					model.negativeLight=decimal.Parse(row["negativeLight"].ToString());
				}
				if(row["positiveLight"]!=null && row["positiveLight"].ToString()!="")
				{
					model.positiveLight=decimal.Parse(row["positiveLight"].ToString());
				}
				if(row["note"]!=null)
				{
					model.note=row["note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select DetectItem,ItemCode,RRsToplimit,RRsLowlimit,HighLimit,LowLimit,unit,qualitativeA,qualitativeB,qualitativeC,qualitativeD,QualitativeFormat,negativeLight,positiveLight,note ");
			strSql.Append(" FROM ResultReference ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return SQLiteHelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM ResultReference ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = SQLiteHelper.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.DetectItem desc");
			}
			strSql.Append(")AS Row, T.*  from ResultReference T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return SQLiteHelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@tblName", DbType.VarChar, 255),
					new SQLiteParameter("@fldName", DbType.VarChar, 255),
					new SQLiteParameter("@PageSize", DbType.Int32),
					new SQLiteParameter("@PageIndex", DbType.Int32),
					new SQLiteParameter("@IsReCount", DbType.bit),
					new SQLiteParameter("@OrderType", DbType.bit),
					new SQLiteParameter("@strWhere", DbType.VarChar,1000),
					};
			parameters[0].Value = "ResultReference";
			parameters[1].Value = "DetectItem";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return SQLiteHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

